<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ShopUserLog extends Model
{
    use HasFactory;

    protected $table = 'sp_shop_user_log';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //操作日志
    protected function addLog($admin_id, $member_id, $operation_type, $operation_explain = '', $before_data = [], $after_data = [], $content = '')
    {
        if ($operation_explain == '编辑供应商') {
            $fix_data = array();
            foreach ($after_data as $lk => $lv){
                if ($lk == 'photo') {
                    $fix_data['店铺头像'] = $lv;
                }
                if ($lk == 'shopname') {
                    $fix_data['店铺名称'] = $lv;
                }
                if ($lk == 'esign_seal_id') {
                    $fix_data['E签宝印章id'] = $lv;
                }
                if ($lk == 'account') {
                    $fix_data['账户类型'] = ShopUser::getAccountName($lv);
                }
                if ($lk == 'account_type') {
                    $fix_data['是否风控'] = ShopUser::isAccountType($lv);
                }
                if ($lk == 'faptype') {
                    $fix_data['发票类型'] = ShopUser::getFaptypeName($lv);
                }
                if ($lk == 'shopcity') {
                    $fix_data['当前经营位置'] = $lv;
                }
                if ($lk == 'people') {
                    $fix_data['店铺联系人'] = $lv;
                }
                if ($lk == 'contact_tel') {
                    $fix_data['联系人手机'] = $lv;
                }
                if ($lk == 'mobile') {
                    $fix_data['客服电话'] = $lv;
                }
                if ($lk == 'contract_type') {
                    $fix_data['签署合同类型'] = ShopUser::getContractTypeName($lv);
                }
                if ($lk == 'sign_num') {
                    $fix_data['签署方式'] = ShopUser::getSignNumName($lv);
                }
                if ($lk == 'mechanism') {
                    $fix_data['公司名称'] = $lv;
                }
                if ($lk == 'companyid') {
                    $fix_data['工商注册号'] = $lv;
                }
                if ($lk == 'company_area') {
                    $fix_data['公司所在区域'] = $lv;
                }
                if ($lk == 'company') {
                    $fix_data['公司地址'] = $lv;
                }
                if ($lk == 'faname') {
                    $fix_data['法人姓名'] = $lv;
                }
                if ($lk == 'idcard') {
                    $fix_data['法人身份证号'] = $lv;
                }
                if ($lk == 'shopcard') {
                    $fix_data['营业执照'] = $lv;
                }
                if ($lk == 'doorphoto') {
                    $fix_data['公司门头照'] = $lv;
                }
                if ($lk == 'seal_url') {
                    $fix_data['公章图片'] = $lv;
                }
                if ($lk == 'faidcard') {
                    $fix_data['身份证正面照'] = $lv;
                }
                if ($lk == 'faidcard_back') {
                    $fix_data['身份证反面照'] = $lv;
                }
                if ($lk == 'endisable') {
                    $fix_data['是否启用'] = ShopUser::isEndisable($lv);
                }
                if ($lk == 'is_city_location') {
                    $fix_data['是否开启城市定位'] = ShopUser::isOpenOrClose($lv);
                }
                if ($lk == 'payeename') {
                    $fix_data['支付宝姓名'] = $lv;
                }
                if ($lk == 'alipayaccount') {
                    $fix_data['支付宝账号'] = $lv;
                }
                if ($lk == 'sms_num') {
                    $fix_data['短信剩余数量'] = $lv;
                }
                if ($lk == 'is_guarantee') {
                    $fix_data['是否开启共同承租合同'] = ShopUser::isGuarantee($lv);
                }
                if ($lk == 'guarantee_amount') {
                    $fix_data['共同承租金额'] = $lv;
                }
                if ($lk == 'guarantee_minage') {
                    $fix_data['共同承租年龄下限'] = $lv;
                }
                if ($lk == 'guarantee_maxage') {
                    $fix_data['共同承租年龄上限'] = $lv;
                }
                if ($lk == 'mutual_guarantee') {
                    $fix_data['是否允许互相承租'] = ShopUser::isMutualGuarantee($lv);
                }
                if ($lk == 'contract_num') {
                    $fix_data['合同剩余数量'] = $lv;
                }
                if ($lk == 'report_bill') {
                    $fix_data['线下报单融资功能'] = ShopUser::isOpenOrClose($lv);
                }
                if ($lk == 'ant_shield_risk_score') {
                    $fix_data['蚁盾风控分数'] = $lv;
                }
                if ($lk == 'is_automatic_report_bill') {
                    $fix_data['是否开启自动报单同步'] = ShopUser::isOpenOrClose($lv);
                }
                if ($lk == 'email') {
                    $fix_data['店铺邮箱'] = $lv;
                }
                if ($lk == 'risk_credit_status') {
                    $fix_data['百融风控状态'] = ShopUser::isOpenOrClose($lv);
                }
                if ($lk == 'risk_credit_amount') {
                    $fix_data['百融风控剩余查询次数'] = $lv;
                }
                if ($lk == 'risk_credit_amount_boolea') {
                    $fix_data['风控大数据状态'] = ShopUser::isOpenOrClose($lv);
                }
                if ($lk == 'risk_credit_amount_boolea') {
                    $fix_data['风控大数据剩余查询次数'] = $lv;
                }
                if ($lk == 'risk_radar_amount_credit') {
                    $fix_data['雷达风控状态'] = ShopUser::isOpenOrClose($lv);
                }
                if ($lk == 'risk_radar_amount_credit') {
                    $fix_data['雷达风控剩余查询次数'] = $lv;
                }
                if ($lk == 'risk_edu_amount_credit') {
                    $fix_data['学历风控状态'] = ShopUser::isOpenOrClose($lv);
                }
                if ($lk == 'risk_edu_amount_credit') {
                    $fix_data['学历风控剩余查询次数'] = $lv;
                }
                if ($lk == 'risk_cre_amount') {
                    $fix_data['征信风控状态'] = ShopUser::isOpenOrClose($lv);
                }
                if ($lk == 'risk_cre_amount') {
                    $fix_data['征信风控剩余查询次数'] = $lv;
                }
            }
            $content = !empty($fix_data) ? urldecode(http_build_query($fix_data)) : '';
            $content = '编辑：' . $content;
        }elseif ($operation_explain == '删除供应商'){
            $shopUser = ShopUser::GetShopUser(array('member_id' => $member_id));
            $before_data = array('is_delete' => ShopUser::SHOP_USER_NOT_DELETE);
            $after_data = array('is_delete' => ShopUser::SHOP_USER_IS_DELETE);
            $content = '删除' . $shopUser['shopname'];
        }
        $addData = array(
            'admin_id' => $admin_id, //操作人id
            'member_id' => $member_id, //member_id
            'operation_type' => $operation_type, //操作类型  1管理后台  2供应商后台
            'operation_explain' => $operation_explain, //操作说明
            'before_data' => $before_data ? json_encode($before_data) : '', //操作前数据
            'after_data' => $after_data ? json_encode($after_data) : '', //操作后数据
            'content' => $content, //操作详情
            'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '', //UA
            'user_ip' => getRealIp(), //IP
            'create_time' => date("Y-m-d H:i:s"), //操作时间
        );
        return $data = $this->insert($addData);
    }

    protected function GetLog($where)
    {
        $data = $this
            ->where($where)
            ->orderBy('create_time', 'desc')
            ->limit(10)
            ->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }


}
